package com.api.weavernorth.action.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

//import com.weavernorth.util.JdeUtil;
import org.apache.commons.logging.Log;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//社保
//public class SBobj {
//
//    public String id;
//    public String bxlx;
//    public String bxlxdm;
//    public String xy;
//    public String z;
//    public String jb;
//}




@Path("/weavernorth/action/database/AutowxyjAction")
public class AutowxyjAction {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(AutowxyjAction.class.getName());

    @GET
    @Path("/getSBData")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSBData(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        log.info("社保查询数据库开始");


        String sbjndValue = Util.null2String(request.getParameter("sbjnd"));  //社保缴纳地

        String hklxValue = Util.null2String(request.getParameter("hklx"));//户口类型

        String rsfwValue = Util.null2String(request.getParameter("rsfw")); //人事范围


        log.info("社保缴纳地:" + sbjndValue + "\n");
        log.info("户口类型:" + hklxValue + "\n");
        log.info("人事范围:" + rsfwValue + "\n");


        //报文请求体
        StringBuffer requestXml;
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet hklxrs=new RecordSet();
        RecordSet rsfwrs=new RecordSet();


        String hklx_xy="";
        String rsfw_xy="";

        JSONObject jsonObject = new JSONObject();

        List<JSONObject> jsobj_list=new ArrayList<>();;

        //查询表的sql

        String hklxstrSql = "select xy from uf_hklxmb t  where hklx= " + hklxValue;  //户口类型
        log.info("失业保险查询户口类型码表sql:" + hklxstrSql);
        hklxrs.executeQuery(hklxstrSql);


        if (hklxrs.next()) {

            //行业编码
            hklx_xy = Util.null2String(hklxrs.getString("xy"));

            //jsonObject.put("hklx", hy);

            log.info("户口类型-行业编码：" + hklx_xy);
        }




        String rsfwstrSql = "select xy from uf_rsfw t  where id= '" + rsfwValue+"'";  //人事范围
        log.info("工伤保险查询人事范围码表sql:" + rsfwstrSql);
        rsfwrs.executeQuery(rsfwstrSql);

        if (rsfwrs.next()) {

            //行业编码
            rsfw_xy = Util.null2String(rsfwrs.getString("xy"));

            //jsonObject.put("rsfw", hy);

            log.info("人事范围-行业编码：" + rsfw_xy);
        }



        //社保码表
        String strSql= "select *  from UF_SBMB_DT1 t where MAINID = " + sbjndValue +"order by bxlx";  //社保缴纳地
        log.info("社保码表sql:" + strSql);
        rs.executeQuery(strSql);


        while (rs.next()) {

            //行业编码
            String id = Util.null2String(rs.getString("id"));
            String bxlx = Util.null2String(rs.getString("bxlx"));
            String bxlxdm = Util.null2String(rs.getString("bxlxdm"));
            String xy = Util.null2String(rs.getString("xy"));
            String z = Util.null2String(rs.getString("z"));
            String jb = Util.null2String(rs.getString("jb"));



            JSONObject jsboj = new JSONObject();
//


            jsboj.put("id",id);
            jsboj.put("bxlx",bxlx);
            jsboj.put("bxlxdm",bxlxdm);


            if(bxlx.equals("1")){
                jsboj.put("xy",hklx_xy);
                log.info("编码1-xy:：" + hklx_xy);
            }
            else if(bxlx.equals("3")) {
                jsboj.put("xy",rsfw_xy);
                log.info("编码3-xy:：" + rsfw_xy);
            }
            else{
                jsboj.put("xy",xy);
            }


            jsboj.put("z",z);
            jsboj.put("jb",jb);


            jsobj_list.add(jsboj);


            //jsonObject.put("hklx", hy);

            log.info("户口类型-行业编码：" + jsboj.toString());
        }





        //response.getWriter().print(jsonObject.toString());

        //return jsonObject.toString();

        log.info("户口类型-行业编码list：" + jsobj_list.toString());

        return jsobj_list.toString();
        //return "success";
    }


    //公积金信息查询

    @GET
    @Path("/getGJJData")
    @Produces(MediaType.TEXT_PLAIN)
    public String getGJJData(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        log.info("公积金查询数据库开始");


        String gjjjndValue = Util.null2String(request.getParameter("gjjjnd"));  //社保缴纳地

        //String hklxValue = Util.null2String(request.getParameter("hklx"));//户口类型

        String rsfwValue = Util.null2String(request.getParameter("rsfw")); //人事范围


        log.info("公积金缴纳地:" + gjjjndValue + "\n");
        //log.info("户口类型:" + hklxValue + "\n");
        log.info("人事范围:" + rsfwValue + "\n");


        //报文请求体
        StringBuffer requestXml;
        //数据库操作
        RecordSet rs = new RecordSet();
       // RecordSet hklxrs=new RecordSet();
        RecordSet rsfwrs=new RecordSet();


        String hklx_xy="";
        String rsfw_xy="";

        JSONObject jsonObject = new JSONObject();

        List<JSONObject> jsobj_list=new ArrayList<>();;

        //查询表的sql


        String rsfwstrSql = "select xy from uf_rsfw t  where id= " + rsfwValue;  //人事范围
        log.info("公积金查询人事范围码表sql:" + rsfwstrSql);
        rsfwrs.executeQuery(rsfwstrSql);

        if (rsfwrs.next()) {

            //行业编码
            rsfw_xy = Util.null2String(rsfwrs.getString("xy"));

            //jsonObject.put("rsfw", hy);

            log.info("人事范围-行业编码：" + rsfw_xy);
        }



        //社保码表
        String strSql= "select *  from UF_GJJMB_DT1 t where MAINID = " + gjjjndValue ;  //社保缴纳地
        log.info("公积金码表sql:" + strSql);
        rs.executeQuery(strSql);


        while (rs.next()) {

            //行业编码
            String id = Util.null2String(rs.getString("id"));
            String bxlx = Util.null2String(rs.getString("bxlx"));
            String bxlxdm = Util.null2String(rs.getString("bxlxdm"));
            String xy = Util.null2String(rs.getString("xy"));
            String z = Util.null2String(rs.getString("z"));
            String jb = Util.null2String(rs.getString("jb"));



            JSONObject jsboj = new JSONObject();
//


            jsboj.put("id",id);
            jsboj.put("bxlx",bxlx);
            jsboj.put("bxlxdm",bxlxdm);


            jsboj.put("xy",rsfw_xy);
            log.info("公积金-xy:：" + rsfw_xy);


            jsboj.put("z",z);
            jsboj.put("jb",jb);


            jsobj_list.add(jsboj);


            //jsonObject.put("hklx", hy);

            log.info("公积金信息：" + jsboj.toString());
        }





        //response.getWriter().print(jsonObject.toString());

        //return jsonObject.toString();

        log.info("公积金信息list：" + jsobj_list.toString());

        return jsobj_list.toString();
        //return "success";
    }


}

















